package pl.edu.mimuw.irs.rcp.controller.editor.workflow.dnd;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartViewer;
import org.eclipse.gef.Request;
import org.eclipse.gef.dnd.AbstractTransferDropTargetListener;
import org.eclipse.gef.requests.CreateRequest;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;

import pl.edu.mimuw.irs.core.model.design.resource.Requirement;
import pl.edu.mimuw.irs.rcp.controller.editor.workflow.part.IRSRequirementEditPart;

public class TextTransferDropTargetListener extends
		AbstractTransferDropTargetListener {

	// *** Attributes

	private RequirementFactory factory = new RequirementFactory();
	
	// *** Constructors
	
	public TextTransferDropTargetListener(EditPartViewer viewer, Transfer xfer) {
		super(viewer, xfer);
	}

	public TextTransferDropTargetListener(EditPartViewer viewer) {
		super(viewer, TextTransfer.getInstance());
	}

	// *** Methods
	
	@Override
	protected void updateTargetRequest() {
	  	((CreateRequest)getTargetRequest()).setLocation(getDropLocation());
	}
	
	protected Request createTargetRequest() {
		CreateRequest request = new CreateRequest();
		request.setFactory(factory);
		return request;
	}
	
	protected void handleDragOver() {
	   	if (getTargetEditPart() instanceof IRSRequirementEditPart) {
			getCurrentEvent().detail = DND.DROP_COPY;
	   	} else {
	   		getCurrentEvent().detail = DND.DROP_NONE;
	   	}
	   	super.handleDragOver();
	}

	@Override
	protected void handleEnteredEditPart() {
		super.handleEnteredEditPart();
	}
	
	protected void handleDrop() {

	   	String s = (String)getCurrentEvent().data;
	   	factory.setId(s);
	   	super.handleDrop();
	   	EditPart editPart = getTargetEditPart();
	   	if (editPart instanceof IRSRequirementEditPart) {
	   		IRSRequirementEditPart irsEditPart = (IRSRequirementEditPart)editPart;
	   		irsEditPart.attachRequirement((Requirement) factory.getNewObject());
	   	}
	}
}
